let router = require('koa-router')()
let resVel = require('../util/ResValue')
let Where = require("../util/SqlWhere")

/**
 * @swagger
 * /api/music/GetMusicList:
 *   get:
 *     tags:
 *       - 音乐
 *     summary: 音乐列表，音乐搜索
 *     description: 获取音乐列表或根据条件搜索音乐。
 *     produces:
 *       - application/json
 *     parameters:
 *       - in: query
 *         name: title
 *         description: 音乐标题关键字，可选。
 *         required: false
 *         schema:
 *           type: string
 *       - in: query
 *         name: SingerName
 *         description: 歌手名称关键字，可选。
 *         required: false
 *         schema:
 *           type: string
 *       - in: query
 *         name: subheading
 *         description: 音乐副标题关键字，可选。
 *         required: false
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: 成功
 *         schema:
 *           type: object
 *           properties:
 *             total:
 *               type: integer
 *               description: 总音乐数量
 *             data:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/Music'
 *       500:
 *         description: 服务器内部错误
 *       default:
 *         description: 意外错误
 */
//音乐列表，音乐搜索
router.get("/GetMsicList", async (ctx, next) => {
    const body = ctx.request.query;
    const whereNew = new Where(`SELECT * FROM music`, body);
    whereNew.addQueryCriteriaMultiple(['%title%','%SingerName%','%subheading%']);
    //设置分页
    whereNew.setPagination(true);
    const list = await whereNew.executeTheQuery();
    //获取总数
    whereNew.setQueryHeader('SELECT COUNT(*) FROM music');
    const total = await whereNew.executeTheQuery();
    ctx = await resVel.success(ctx);
    ctx.body.total = total.length === 0 ? 0 : total[0]["COUNT(*)"];
    ctx.body.data = list;
}).noAuth()

/**
 * @swagger
 * /api/user/collection:
 *   post:
 *     tags:
 *       - 音乐
 *     summary: 查询用户收藏
 *     description: 查询用户收藏的音乐列表。
 *     produces:
 *       - application/json
 *     requestBody:
 *       description: 请求体参数
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               userId:
 *                 type: string
 *                 description: 用户ID
 *     responses:
 *       200:
 *         description: 成功
 *         schema:
 *           type: object
 *           properties:
 *             data:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/Music'
 *       500:
 *         description: 服务器内部错误
 *       default:
 *         description: 意外错误
 */
//查询用户收藏(客户端)
router.post('/user/collection',async (ctx,next)=>{
    const body = ctx.request.body
    const whereNew = new Where('SELECT * FROM user_music JOIN  music ON  user_music.mucisid = music.id',body) 
    ctx = await resVel.success(ctx);
    ctx.body.data = await whereNew.executeTheQuery();
}).noAuth()


/**
 * @swagger
 * /api/music/Music:
 *    get:
 *      tags:
 *      - 音乐
 *      summary: 获取音乐详情
 *      produces:
 *      - application/json
 *      parameters:
 *        - in: query
 *          name: id
 *          schema:
 *            type: integer
 *      responses:
 *        200:
 *          description: Success
 *          schema:
 *            type: object
 *            properties:
 *              data:
 *                type: array
 *                items:
 *                  $ref: '#/components/schemas/Music'
 *        500:
 *          description: Internal Server Error
 *        default:
 *          description: Unexpected error
 */
//音乐详情
router.get('/Music',async (ctx,next)=>{
    const body = ctx.request.query
    const whereNew = new Where('SELECT * FROM music',body)
    whereNew.addQueryCriteriaMultiple(['id'])
    ctx = await resVel.success(ctx);
    ctx.body.data = await whereNew.executeTheQuery();
}).noAuth()


/**
 * @swagger
 * /api/music/addMusic:
 *    post:
 *      tags:
 *      - 音乐
 *      summary: 新增音乐
 *      produces:
 *      - application/json
 *      parameters:
 *      - name: title
 *        description: 歌曲名称
 *        type: string
 *      - name: img
 *        description: 图片地址
 *        type: string
 *      - name: SingerName
 *        description: 歌手名称
 *        type: string
 *      - name: SongTime
 *        description: 发布时间
 *        type: string
 *      - name: Plays
 *        description: 播放次数
 *        type: integer
 *      - name: subheading
 *        description: 副标题
 *        type: string
 *      - name: Musicurl
 *        description: 音频地址
 *        type: string
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                title:
 *                  type: string
 *                img:
 *                  type: string
 *                SingerName:
 *                  type: string
 *                SongTime:
 *                  type: string
 *                Plays:
 *                  type: integer
 *                subheading:
 *                  type: string
 *                Musicurl:
 *                  type: string
 *      responses:
 *        200:
 *          description: Success
 *          schema:
 *            type: object
 *            properties:
 *              data:
 *                $ref: '#/components/schemas/Music'
 *        500:
 *          description: Internal Server Error
 *        default:
 *          description: Unexpected error
 */
// 新增音乐
router.post('/addMusic', async (ctx, next) => {
    const body = ctx.request.body
    const whereNew = new Where(`INSERT INTO music (title, img, SingerName, SongTime, Plays, subheading, Musicurl) VALUES ('${body.title}','${body.img}','${body.SingerName}','${body.SongTime}',${body.Plays},'${body.subheading}', '${body.Musicurl}')`,body)
    ctx = await resVel.success(ctx);
    ctx.body.data = await whereNew.executeTheQuery();
}).noAuth()


/**
 * @swagger
 * /api/music/udpateMusic:
 *    put:
 *      tags:
 *      - 音乐
 *      summary: 修改音乐
 *      produces:
 *      - application/json
 *      parameters:
 *      - name: id
 *        description: 歌曲id
 *        type: integer
 *      - name: title
 *        description: 歌曲名称
 *        type: string
 *      - name: img
 *        description: 图片地址
 *        type: string
 *      - name: SingerName
 *        description: 歌手名称
 *        type: string
 *      - name: SongTime
 *        description: 发布时间
 *        type: string
 *      - name: Plays
 *        description: 播放次数
 *        type: integer
 *      - name: subheading
 *        description: 副标题
 *        type: string
 *      - name: Musicurl
 *        description: 音频地址
 *        type: string
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              type: object
 *              properties:
 *                id:
 *                  type: integer
 *                title:
 *                  type: string
 *                img:
 *                  type: string
 *                SingerName:
 *                  type: string
 *                SongTime:
 *                  type: string
 *                Plays:
 *                  type: integer
 *                subheading:
 *                  type: string
 *                Musicurl:
 *                  type: string
 *      responses:
 *        200:
 *          description: Success
 *          schema:
 *            type: object
 *            properties:
 *              data:
 *                $ref: '#/components/schemas/Music'
 *        500:
 *          description: Internal Server Error
 *        default:
 *          description: Unexpected error
 */
// 修改音乐
router.put('/udpateMusic', async (ctx, next) => {
    const body = ctx.request.body
    const whereNew = new Where(`UPDATE music SET title = '${body.title}', img = '${body.img}', SingerName = '${body.SingerName}', SongTime = '${body.SongTime}', Plays = ${body.Plays}, subheading = '${body.subheading}', Musicurl = '${body.Musicurl}' WHERE id = ${body.id}`,body)
    ctx = await resVel.success(ctx);
    ctx.body.data = await whereNew.executeTheQuery();
}).noAuth()


/**
 * @swagger
 * /api/music/delMusic:
 *    delete:
 *      tags:
 *      - 音乐
 *      summary: 删除音乐
 *      produces:
 *      - application/json
 *      parameters:
 *        - in: query
 *          name: id
 *          required: true
 *          schema:
 *            type: integer
 *      responses:
 *        200:
 *          description: Success
 *          schema:
 *            type: object
 *            properties:
 *              data:
 *                $ref: '#/components/schemas/Music'
 *        500:
 *          description: Internal Server Error
 *        default:
 *          description: Unexpected error
 */
// 删除音乐
router.delete('/delMusic', async (ctx, next) => {
    const body = ctx.request.query
    const whereNew = new Where(`DELETE FROM music WHERE id = ${body.id}`,body)
    ctx = await resVel.success(ctx);
    ctx.body.data = await whereNew.executeTheQuery();
}).noAuth()


//用户点击收藏
router.post('/user/click/collection',async (ctx,next)=>{
    const body = ctx.request.body
    const whereNew = new Where(`INSERT INTO user_music (userid,mucisid) VALUES ('${body.userid}','${body.mucisid}')`,body)
    ctx = await resVel.success(ctx);
    ctx.body.data = await whereNew.executeTheQuery();
}).noAuth()



module.exports = router;
